<?php
class CartController extends Controller{
	public function __construct($data) {
		parent::__construct($data);
	}

    public function loaduser()
	{
		if(isset($_SESSION['USER']['USER_id']))
		{
			$this->Userid=trim($_SESSION['USER']['USER_id']);
		}
		else
		{
			$this->Userid="";
		}

	}
    public $Userid;
	public function index(){
		try
		{
			require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );
			$this->_load_php_file("Class_FUNC.php");
			 $site = Class_FUNC::get_site();
	    if(isset($site['site_keyword']))
	    $this->assign('site_keyword', $site['site_keyword']);
        if(isset($site['site_description']))
	    $this->assign('site_description', $site['site_description']);
	    if(isset($site['site_title']))
	    $this->assign('site_title', '買い物カゴ | '.$site['site_title']);
			
			
		    $this->_load_php_file("chklogin.php");
	        if(!isset($_SESSION['USER'])){
	    	
	       require_once( '../system/cookie.php' );
	    
	        }
            $this->loaduser();
			$M_dao = new Class_M_DAO;
            $strcart;
            require_once( "../system/Class_Cart.php" );

            $strcart=Class_Cart::getCart($this->Userid);
            
			if(!empty($strcart))
			{
                
                $arraycart=Class_Cart::cartStrToArray($strcart);
				$str_where=" 1=0 ";
				foreach ($arraycart as $Key=>$value)
				{
						$str_where = $str_where." or id='". mysql_real_escape_string($Key)."' ";
				}
                $taxtype=$this->getTaxType();
				$tax=0;
                if($taxtype!=1)
                {
                    $tax=$this->getTax();
                }
                
				$sql = "SELECT *,0 as orderNum FROM  d_goods  where ".$str_where;
				$good_data = $M_dao->get_rows($sql);
				$newcart="";
				foreach ($good_data as &$value)
				{
					$value['orderNum']=$arraycart[(string)$value['id']];
					if($newcart=="")
					{
						$newcart =$value['id']."-".$value['orderNum'];
					}
					else
					{
						$newcart =$newcart."|".$value['id']."-".$value['orderNum'];
					}
				}
				Class_Cart::setCart($this->Userid,$newcart);
				//add by zuo
				$this->_load_php_file("Class_FUNC.php");
			 foreach ($good_data as &$value){
			 	if(isset($value['goods_image']) && !empty($value['goods_image'])){
			 		$value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],3);
			 	}
                 if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){

                     $value['goods_unitprice'] = round($value['goods_unitprice']*(1+$tax)); 
                 }
			 }
			 //add by zuo end

				if (isset($good_data))
					$this->assign('good_data', $good_data);

			}
			else
			{
				//echo "<script>history.go(-1);</script>";

			}
		}
		catch(Exception $e)
		{
			$this->assign('errmsg',$e->getMessage());
			//echo $e;
		}


		$this->_load_mdao_class("t_m_goods");
		$goods_dao = new t_m_goods;
		//
		$sql = "SELECT b.id,b.goods_name,b.goods_image,b.goods_unitprice FROM d_good_rank a inner JOIN d_goods b ON a.good_id = b.id ORDER BY a.rank";
		$charts_data = $goods_dao->get_rows($sql);
		foreach ($charts_data as &$value){
			if(isset($value['goods_image']) && !empty($value['goods_image'])){
				$value['goods_image'] = Class_FUNC::get_imagepath($value['id'],$value['goods_image'],1);
			}
		 if(isset($value['goods_unitprice']) && !empty($value['goods_unitprice'])){
	          	 $value['goods_rate'] = $value['goods_unitprice']+$value['goods_unitprice'] * RATE;
	          }
		}
		if (isset($charts_data))
			$this->assign('charts_data',$charts_data);

	    $this->assign('pc_meta', $this->fetch('pc/pc_meta.html'));
		$this->assign('chartsing', $this->fetch('pc/charts.html'));
		$this->assign('pc_head', $this->fetch('pc/pc_head.html'));
		$this->assign('pc_foot', $this->fetch('pc/pc_foot.html'));
		$this->display('pc/cart.html');
	}

    public function getTaxType()
    {
    	require_once( '../system/ecjp_m.php' );
        require_once( "../system/Class_M_DAO.php" );
        
        $M_dao = new Class_M_DAO;

        $sql3 = "SELECT flag_value FROM m_setflag WHERE flag_id=1";

        $order_tax = $M_dao->get_rows($sql3);

        return $order_tax[0]['flag_value'];
    }
    
	public function getTax()
	{

			$temptax=0;

			require_once( '../system/ecjp_m.php' );
			require_once( "../system/Class_M_DAO.php" );

			$M_dao = new Class_M_DAO;

			$sql3 = "SELECT flag_value FROM m_setflag WHERE flag_id=2";

			$order_tax = $M_dao->get_rows($sql3);

			//	print_r ($order_tax);

			$temptax=$order_tax[0]['flag_value'];

			return  $temptax/100;

	}
	public function ajaxgetcart()
	{
        $this->loaduser();
        require_once( "../system/Class_Cart.php" );
        echo Class_Cart::getCart($this->Userid);
	}

	public function ajaxsetcart()
	{
        $this->loaduser();
		require_once( '../system/ecjp_m.php' );
		require_once( '../system/Class_ERROR.php' );
		$oerr = new Class_ERROR('','');
		$temp=isset($this->p['shopjpcart'])?$this->p['shopjpcart']:"";
		if($temp=="")
		{
            require_once( "../system/Class_Cart.php" );
			Class_Cart::setCart($this->Userid,"");
			echo "cart ajax success";return;
		}
		else
		{
			//check
			$line=explode("|",$temp);
			$errjs="";
			$errmsg="";
			foreach ($line as $value)
			{
				if($value!="")
				{
					$temp=explode('-',$value);
					$num=(string)$temp[1];
					$errmsg = $oerr->check( $num, array(CHK_NUM));
					if($errmsg!="")
					{
						$errjs.="$('#errmsg_".$temp[0]."').html('".$errmsg."');";
					}
				}
			}
			if($errjs=="")
			{
                require_once( "../system/Class_Cart.php" );
                
				Class_Cart::setCart($this->Userid,isset($this->p['shopjpcart'])?$this->p['shopjpcart']:"");
                
				echo "cart ajax success";
			}
			else
			{
				echo $errjs;
			}
		}

	}

	public function test()
	{
		$this->display('pc/testgoods.html');

	}


}